SET SERVEROUTPUT ON SIZE 1000000;
SET VERIFY OFF

DECLARE
	l_total_extracted	NUMBER;
	l_total_imported	NUMBER;	
	l_total_extracted_2	NUMBER;
	l_total_imported_2	NUMBER;			
	l_func_id			VARCHAR2(100):='CAPARREC_AUTO_ACCOUNTING';
	l_code VARCHAR2(100):=UPPER('&1');
	l_code2 VARCHAR2(100):=UPPER('&1');
BEGIN
   select set_of_books_id 
    into l_code
    from gl_sets_of_books 
    where upper(short_name) = upper(l_code);
	
	SELECT COUNT(1)
	INTO l_total_extracted        
	from   RA_ACCOUNT_DEFAULTS_ALL rad,
           hr_organization_information hoi
    where  hoi.org_information_context = 'Operating Unit Information'
    and    hoi.org_information3 = l_code
    and    hoi.organization_id = rad.org_id ;
	
	SELECT COUNT(1) 
	INTO l_total_imported
	FROM CRST_RA_ACCOUNT_DEFAULTS_ALL;

	SELECT COUNT(1)
	INTO l_total_extracted_2        
	from   ra_account_default_segments
	where  gl_default_id iN (select gl_default_id
		   				     from   RA_ACCOUNT_DEFAULTS_ALL rad,
							 		hr_organization_information hoi
						    where  hoi.org_information_context = 'Operating Unit Information'
							and    hoi.org_information3 = (
							  							select set_of_books_id 
											 		    from gl_sets_of_books 
														where upper(short_name) = l_code2
														)
							and    hoi.organization_id = rad.org_id );

	SELECT COUNT(1) 
	INTO l_total_imported_2
	FROM CRST_RA_ACCOUNT_DEFAULT_SEGMEN;

	dbms_output.put_line('Function: '||l_func_id);		
	dbms_output.put_line('Total Extracted: '||TO_CHAR(l_total_extracted)) ;
	dbms_output.put_line('Total Imported:  '||TO_CHAR(l_total_imported));
	dbms_output.put_line('Total Extracted_2: '||TO_CHAR(l_total_extracted_2)) ;
	dbms_output.put_line('Total Imported_2:  '||TO_CHAR(l_total_imported_2));
	IF l_total_extracted <> l_total_imported THEN
		dbms_output.put_line('Error: No. of Records do not match!');
	END IF;	
	IF l_total_extracted_2 <> l_total_imported_2 THEN
		dbms_output.put_line('Error: No. of Records do not match!');
	END IF;	
						
EXCEPTION
	WHEN OTHERS THEN	
		dbms_output.put_line('Function Error: '||l_func_id);			
END;
/
